//
//  UserLondManager.m
//  Yiqidianyin
//
//  Created by apple on 15/8/9.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "UserLondManager.h"
#import "AFNetworking.h"
#import "MyMD5.h"
#import "CommonCrypto/CommonDigest.h"
#import "UserLondManager+Fun.h"
@interface UserLondManager()

@property (nonatomic,strong)AFHTTPRequestOperationManager *afManager;
@property(nonatomic,strong)UserLondManager *LondM;
@end

@implementation UserLondManager

+(instancetype)share
{
    static UserLondManager *m = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        m = [[UserLondManager alloc]init];
    });
    return m;
}
-(void)longwithUserToken:(NSString *)userToken blockHandle:(LondManagerBlock)bh
{
    self.afManager = [[AFHTTPRequestOperationManager alloc]init];
    self.afManager.responseSerializer = [AFHTTPResponseSerializer serializer];

    NSString * url =[http stringByAppendingString:@"/mobile/user/verifyToken"];
    NSDictionary *dic = @{@"token":userToken};
    AFHTTPRequestOperation * op = [self.afManager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSDictionary *dic = obj;
        bh(dic);
        NSLog(@"%@",dic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    [op start];

    
}

-(void)londWithUserNum:(NSString *)usernum andPassWord:(NSString *)password BlockHandle:(LondManagerBlock)bh
{
    self.afManager = [[AFHTTPRequestOperationManager alloc]init];
    self.afManager.responseSerializer = [AFHTTPResponseSerializer serializer];

    NSString * url =[http stringByAppendingString:@"/mobile/user/login"];
    
    NSString *md5PassWord = [UserLondManager md5:password];
    
        NSUserDefaults *md5 = [NSUserDefaults standardUserDefaults];
    
        [md5 setObject:md5PassWord forKey:@"md5PassW"];
    
        [md5 synchronize];
   
    
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"UserLond"];
    
    [vc setValue:md5PassWord forKey:@"md5passw"];
    NSDictionary * dic =  @{@"username":usernum,@"password":md5PassWord};
    
    
    
    AFHTTPRequestOperation * op = [self.afManager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSDictionary *dic = obj;
        bh(dic);
        
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    [op start];

}
-(void)fagetaWithPhonID:(NSString *)poneID andNewPassWord:(NSString *)passWord andCode:(NSString *)code BlockHandle:(LondManagerBlock)bh
{
    self.afManager = [[AFHTTPRequestOperationManager alloc]init];
    self.afManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString * url =[http stringByAppendingString:@"/mobile/user/findMyPass"];
    
    NSString *md5PassWord = [UserLondManager md5:passWord];
    
    NSDictionary * dic =  @{@"username":poneID,@"password":md5PassWord,@"code":code};
    AFHTTPRequestOperation * op = [self.afManager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSDictionary *dic = obj;
        bh(dic);
        NSLog(@"%@",dic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    [op start];

}


-(void)forgetWithPhonID:(NSString *)phonID BackBlock:(LondManagerBlock)bh
{
    self.afManager = [[AFHTTPRequestOperationManager alloc]init];
    self.afManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString * url =[http stringByAppendingString:@"/mobile/user/sendMobileMessage"];
    
    
    
    NSDictionary * dic =  @{@"phoneNum":phonID};
    
    
    AFHTTPRequestOperation * op = [self.afManager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {

        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        bh(dic);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
    [op start];

}
+(NSString *)md5: (NSString *) inPutText
{
    const char *cStr = [inPutText UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, strlen(cStr), result);
    
    return [[NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
             result[0], result[1], result[2], result[3],
             result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],
             result[12], result[13], result[14], result[15]
             ] lowercaseString];
}


@end
